import sys

import pygame

from settings import Settings
from ship import Ship
from bullet import Bullet


class AlienInvasion:
    """管理游戏资源和行为的类"""

    def __init__(self):
        """初始化游戏并创建游戏资源"""

        pygame.init()

        # 控制帧率
        self.clock = pygame.time.Clock()
        # 初始化时导入Settings类
        self.settings = Settings()

        # self.screen = pygame.display.set_mode(
        #     (self.settings.screen_width, self.settings.screen_height))

        # 全屏游戏
        self.screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height

        pygame.display.set_caption('Alien Invasion')

        # 添加飞船
        self.ship = Ship(self)
        # 子弹组
        self.bullets = pygame.sprite.Group()

    def run_game(self):
        """开始游戏的主循环"""
        while True:
            # 侦听键盘和鼠标事件
            self._check_events()

            # # 每次循环时都重绘屏幕
            # self.screen.fill(self.settings.bg_color)
            # # 绘制飞船的位置
            # self.ship.blitme()
            # # 让最近绘制的屏幕可见
            # pygame.display.flip()

            # 更新屏幕
            self.ship.update()
            # 更新子弹
            self.bullets.update()
            self._update_screen()
            # 设置帧率60hz
            self.clock.tick(60)

    def _check_events(self):
        """响应按键和鼠标事件"""
        # for event in pygame.event.get():
        #     if event.type == pygame.QUIT:
        #         sys.exit()
        #     elif event.type == pygame.KEYDOWN:
        #         if event.key == pygame.K_RIGHT:
        #             # 向右移动飞船
        #             self.ship.moving_right = True
        #         elif event.key == pygame.K_LEFT:
        #             self.ship.moving_left = True
        #     elif event.type == pygame.KEYUP:
        #         if event.key == pygame.K_RIGHT:
        #             self.ship.moving_right = False
        #         elif event.key == pygame.K_LEFT:
        #             self.ship.moving_left = False

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)

    def  _check_keydown_events(self,event):
        """响应按下"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        # 按Q退出
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()

    def _check_keyup_events(self,event):
        """响应释放"""
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = False

    def _fire_bullet(self):
        """创建一颗子弹，并将其加入编组bullets"""
        new_bullet = Bullet(self)
        self.bullets.add(new_bullet)

    def _update_screen(self):
        """更新屏幕上的图像，并切换到新屏幕"""
        self.screen.fill(self.settings.bg_color)
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.ship.blitme()

        pygame.display.flip()


if __name__ == '__main__':
    alien = AlienInvasion()
    alien.run_game()
